<?php
	// Smarty
	require_once( './Addons/Smarty/libs/Smarty.class.php' );

	define('TEMPLATE_DIR', './styles/ngen');
	define('COMPILE_DIR', TEMPLATE_DIR . '/compile');
	define('CONFIG_DIR', TEMPLATE_DIR . '/config');
	define('CACHE_DIR', TEMPLATE_DIR . '/cache');

	// NGen
	define('SECTION_DIR', './Sections/');
	define('DEFAULT_SECTION', 'home');
	define('DEFAULT_ACTION', 'main');
	
	class page extends Smarty
	{
		/**
		 * Template file
		 */
		public $tpl = '';
		/**
		 * Section requested
		 */
		public $section = '';
		/**
		 * Action to perform (Section->Action)
		 */
		public $action = '';
		/**
		 * Action Object
		 */
		public $actObj = null;
		/**
		 * NGenCore Object
		 */
		public $core = null;
		/**
		 * Determines whether or not we are reporting a general error
		 */
		public $error = false;
		
		public function __construct( $section, $action, $cache = 0, $time = 86400 )
		{
			// Get section
			$this->getSection(strtolower($this->escape($section)));
			
			//Get Action
			$this->getAction(strtolower($this->escape($action)));
			
			// Initialize Smarty
			$this->Smarty();
			
			// Change the default template directories
			$this->template_dir = TEMPLATE_DIR;
			$this->compile_dir = COMPILE_DIR;
			$this->config_dir = CONFIG_DIR;
			$this->cache_dir = CACHE_DIR;
			
			// Change default caching behavior
			$this->caching = $cache;
			$this->cache_lifetime = $time;
		}
		
		/**
		 * Checks the integrity and length of a variable to ensure we are getting _something_
		 */
		private function isvalid($what)
		{
			if(!isset($what) || empty($what))
			{
				return false;
			}
			return true;
		}
		
		/**
		 * Removes (potentially) dangerous symbols: \ / .
		 */
		private function escape($what)
		{
			return strtr( $what, array( '/' => '', '\\' => '', '.' => '' ) );
		}
		
		/**
		 * Checks the section validity [existance]
		 */
		public function getSection( $section )
		{
			if( !$this->isvalid($section) || !is_dir( SECTION_DIR . $section . '/' ) )
			{
				return $this->section = DEFAULT_SECTION;
			}
			
			return $this->section = $section;
		}
		
		/**
		 * 1) Verifies the action
		 * 2) Forms Action object
		 * @since Feb 12th, 2008
		 * @update Replaced GenErr(&$this) -> GenErr($this), and new Action(&$this) -> new Action($this)
		 */
		public function getAction( $action )
		{
			if( !$this->isvalid($action) || !is_file( SECTION_DIR . $this->section . '/' . $action . '.php' ) )
			{
				$this->action = DEFAULT_ACTION;
			}
			else
			{
				$this->action = $action;
			}

			// Create Action Object
			require_once( SECTION_DIR . $this->section . '/' . $this->action . '.php' );

			// General error, or not?
			if($this->error)
			{
				return $this->actObj = new GenErr($this);
			}

			return $this->actObj = new Action($this);
		}
		
		/**
		 * Redirects the user
		 */
		public function redirect( $where )
		{
			header( 'location: ' . $where );
			exit;
		}
		
		/**
		 * 1) Performs the page action
		 * 2) Displays the page [if not silenced]
		 * @since Feb 12th, 2008
		 * @update Replaced DefaultAction(&$this) -> DefaultAction($this)
		 */
		public function load()
		{
			// We don't have a run() method for general errors
			if(!$this->error)
			{
				// Before we run the desired page.. run default script!
				require_once( SECTION_DIR . '.default/' . DEFAULT_ACTION . '.php' );
				
				// Set the section & action (trakback)
				$this->assign( 'section', $this->section );
				$this->assign( 'action', $this->action );
				
				$default = new DefaultAction($this);
				
				$this->actObj->run();
			}
			
			if(!isset($this->actObj->silence) || $this->actObj->silence != true)
			{
				$this->display( $this->section . '.' . $this->action . '.tpl' );
			}
		}
	}
?>